package controller

import (
	"errors"
	"github.com/gin-gonic/gin"
	"strconv"
)

// 从token获取Id
func getCurrentUserID(ctx *gin.Context) (userID int64, err error) {
	uid, ok := ctx.Get("userID")
	if !ok {
		err = errors.New("用户不存在")
		return
	}
	userID, ok = uid.(int64)
	if !ok {
		err = errors.New("用户ID有误")
		return
	}
	return
}

// 设置分页信息
func getPageInfo(ctx *gin.Context) (page int64, limit int64) {
	pageStr := ctx.Query("page")
	limitStr := ctx.Query("limit")

	var err error
	if page, err = strconv.ParseInt(pageStr, 10, 64); err != nil {
		page = 1
	}
	if limit, err = strconv.ParseInt(limitStr, 10, 64); err != nil {
		limit = 15
	}

	return
}
